package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.mapper.DepartmentMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Department selectByPrimaryKey(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> selectAll() {
        return departmentMapper.selectAll();
    }

    @Override
    public void saveOrUpdate(Department entity) {
        if (entity.getId() != null){
            departmentMapper.updateByPrimaryKey(entity);
        }else {
            entity.setState(true);
            departmentMapper.insert(entity);
        }
    }

    @Override
    public void changeState(Long id) {
        departmentMapper.changeState(id);
    }

    @Override
    public PageResult query(QueryObject qo) {
        int total = departmentMapper.query4Count(qo);
        if(total == 0 ){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<Department> rows = departmentMapper.query4List(qo);
        return new PageResult(total,rows);
    }
}
